package exttable.testversioning;

import exttable.testversioning.testversioncreate.CreateAVMVC;
import exttable.testversioning.testversionrunner.TestVersionView;
import exttable.testversioning.testversionview.ViewAVMVC;
import exttable.view.ViewAnalysis;
import exttable.view.ViewVersion;
import utils.Localisation;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * Created by IntelliJ IDEA.
 * User: Admin
 * Date: Jun 16, 2010
 * Time: 12:34:06 PM
 * To change this template use File | Settings | File Templates.
 */
public class ButtonHandler extends DefaultCellEditor {

    private JButton button;
    private TestVersionView tvr;


    public ButtonHandler(JCheckBox checkBox, TestVersionView tvr) {
        super(checkBox);
        this.tvr = tvr;
    }

    @Override
    public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) {
        ViewAnalysis va = (ViewAnalysis) table.getValueAt(row, 0);
        ViewVersion version = (ViewVersion) table.getValueAt(row, 1);
        return getButtonToCreateVersion(va, version);
    }

    private JButton getButtonToCreateVersion(final ViewAnalysis analysis, final ViewVersion version) {
        button = new JButton(analysis.toString());
        button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                JPopupMenu menu = new JPopupMenu();
                Component c = (Component) e.getSource();
                JMenuItem viewVersions = new JMenuItem(Localisation.getMessage("menuItem.viewversions"));
                JMenuItem createVersions = new JMenuItem(Localisation.getMessage("menuItem.createversions"));
                if (!version.getVersionId().equals("-1")) {
                    menu.add(viewVersions);
                }
                menu.add(createVersions);
                menu.show(c, 50, c.getHeight());

                viewVersions.addActionListener(new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        try {
                        new ViewAVMVC(analysis);
                        }
                        catch(Exception ex){
                            ex.printStackTrace();
                        }
                    }
                });

                createVersions.addActionListener(new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        try {
                        new CreateAVMVC(analysis, tvr);
                        }
                        catch(Exception ex){
                            ex.printStackTrace();
                        }
                    }
                });

            }
        });
        return button;
    }
}
